package inet.app.vod.net.http;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.BaseActivity;
import inet.app.vod.util.MyUtil;
import android.app.Activity;
import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.inet.app.framework.networkutil.NetworkUtil;
import com.inet.app.framework.widget.MyToast;

public abstract class VolleyListener {

	private Response.Listener<String> success;
	private Response.ErrorListener error;
	private Activity activity;
	private Context appContext;

	/**
	 * 
	 * @param context
	 *            Use application context
	 */
	public VolleyListener(Activity acivity) {
		this.activity = acivity;
		appContext = activity.getApplicationContext();
		success = new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				onFinish();
				onSuccess(response);
			}
		};
		error = new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				onFinish();
				onError(error);
			}
		};
	}

	public Response.Listener<String> getSuccessListener() {
		return success;
	}

	public Response.ErrorListener getErrorListener() {
		return error;
	}

	public boolean start() {
		if (!NetworkUtil.getInstance(appContext).isConnect()) {
			if (activity instanceof BaseActivity) {
				MyUtil.showDialog(activity.getString(R.string.error_connect),
						activity);
			} else {
				MyToast.show(activity,
						activity.getString(R.string.error_connect));
			}
			return false;
		}
		onStart();
		return true;
	}

	public abstract void onStart();

	public abstract void onSuccess(String response);

	public abstract void onError(VolleyError error);

	public abstract void onFinish();

}
